# -*- coding: utf-8 -*-
"""
Created on 2022/11/14 上午11:19

@author: chenfei
"""

import sys
sys.path.append('..')
import MyRpc.MyRpcSql as rpc
import json

'''
读取所有的 schema
'''
def get_schemas(user_token):
    root = [];
    rs = rpc.run_sql(user_token, "SELECT SCHEMA_NAME FROM sys.SCHEMAS", "schema");
    my_rs = json.loads(rs);
    for r in my_rs:
        root.append({
            "id": r[0],
            "text": r[0]
        });
    return root;

'''
读取所有的 schema
'''
def get_tables(user_token, schema_name):
    root = [];
    rs = rpc.run_sql(user_token, "SELECT TABLE_NAME FROM sys.TABLES WHERE SCHEMA_NAME = '{0}'".format(schema_name), "schema");
    my_rs = json.loads(rs);
    for r in my_rs:
        root.append({
            "id": r[0],
            "text": r[0],
            "schema": schema_name,
            "leaf": True
        });
    return root;

'''
读取表的列
'''
def read_column_meta(user_token, schema, table_name):
    rs = rpc.run_sql(user_token, "SELECT * FROM {0}.{1}".format(schema, table_name), "meta");
    my_rs = json.loads(rs);
    return my_rs;

'''
判断是否注册
'''
def re_register(group_name):
    rs = rpc.run_sql('', "SELECT m.id FROM MY_META.MY_USERS_GROUP m WHERE m.GROUP_NAME = '{0}'".format(group_name), "schema");
    my_rs = json.loads(rs);
    return my_rs;

'''
登录
'''
def login(user_token):
    rs = rpc.run_sql('', "SELECT m.id FROM MY_META.MY_USERS_GROUP m WHERE m.USER_TOKEN = '{0}'".format(user_token), "schema");
    my_rs = json.loads(rs);
    return my_rs;


'''
注册
'''
def register_db(group_name, user_token):
    rs = rpc.run_sql('', "create schema {0};add_user_group('{0}', '{1}', 'all', '{0}');".format(group_name, group_name + user_token), "my_meta");
    #my_rs = json.loads(rs);
    if rs is None or rs == '':
        return 1;
    return 0;

'''
读取表的 count
'''
def get_table_count(user_token, schema, table_name):
    rs = rpc.run_sql(user_token, "SELECT count(*) FROM {0}.{1}".format(schema, table_name), "count");
    my_rs = json.loads(rs);
    return my_rs;

'''
读取表的 row
'''
def get_table_row(user_token, schema, table_name, start, limit):
    ps = {'start': start, 'limit': limit, 'row': 1};
    rs = rpc.run_sql(user_token, "SELECT * FROM {0}.{1}".format(schema, table_name), json.dumps(ps));
    my_rs = json.loads(rs);
    return my_rs;

'''
执行 select 语句
'''
def run_select_meta(user_token, sql):
    rs = rpc.run_sql(user_token, sql, "meta");
    my_rs = '';
    try:
        my_rs = json.loads(rs);
    except Exception as ex:
        return my_rs;
    return my_rs;

'''
执行 Dawn Sql 语句
'''
def run_dawn_sql(user_token, sql):
    rs = rpc.run_sql(user_token, sql, "");
    try:
        my_rs = json.loads(rs);
        if isinstance(my_rs, dict) and my_rs.__contains__('err'):
            return my_rs;
        return {'msg': rs};
    except Exception as ex:
        return rs;

'''
执行 select 语句
'''
def run_select(user_token, sql, start, limit):
    ps = {'start': start, 'limit': limit, 'select': 1};
    rs = rpc.run_sql(user_token, sql, json.dumps(ps));
    my_rs = json.loads(rs);
    return my_rs;

'''
load code to db
'''
def load_code(user_token, sql):
    rs = rpc.run_sql(user_token, sql, "load");
    if rs is None:
        return {'msg': '保存成功！'};
    else:
        try:
            my_rs = json.loads(rs);
            if isinstance(my_rs, dict) and my_rs.__contains__('err'):
                return {'msg': my_rs.get('err')};
            return {'msg': '保存失败！'};
        except Exception as ex:
            return {'msg': '保存失败！'};
























